package com.edu.stress.test.controller;

import com.edu.stress.test.constant.Constant;
import com.edu.stress.test.service.CheckService;
import com.edu.stress.test.service.DownService;
import com.edu.stress.test.service.RegisterService;
import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient;
import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext;
import org.apache.jmeter.samplers.SampleResult;
import org.apache.log4j.Logger;

/**
 * register+checkVersion+DownloadReport
 */
public class DownController extends AbstractJavaSamplerClient {

    private static final Logger logger = Logger.getLogger(DownController.class);

    public Arguments getDefaultParameters() {
        Arguments params = new Arguments();
        params.addArgument("vin", "");
        return params;
    }

    public SampleResult runTest(JavaSamplerContext context) {

        String vin = context.getParameter("vin");
        SampleResult sr = new SampleResult();
        sr.setSampleLabel("下载上报接口");
        sr.sampleStart();

        Integer beforeReg = Constant.RANDOM.nextInt(Constant.GAP);
        Integer beforeCheck = Constant.RANDOM.nextInt(Constant.GAP);
        Integer beforeDown = Constant.RANDOM.nextInt(Constant.GAP);
        logger.info("vin:"+vin+"。sleep:"+beforeReg+"ms后调用register接口;sleep: "+ beforeCheck +" ms后调用check接口;sleep: "+ beforeDown +" ms后调用down接口。");

        try {
            RegisterService register = new RegisterService(vin);
            Thread.sleep(beforeReg);
            if (register.register()) {
                CheckService check = new CheckService(register.getDeviceId(), register.getSecret());
                Thread.sleep(beforeCheck);
                if (check.check()) {
                    DownService down = new DownService(register.getDeviceId(), register.getSecret(), check.getSessionId());
                    Thread.sleep(beforeDown);
                    if (down.down()) {
                        sr.setResponseData("response:" + down.getResponse(), null);
                        sr.setDataType(SampleResult.TEXT);
                        sr.setResponseMessage(vin);
                        sr.setResponseCodeOK();
                        sr.setSuccessful(true);
                    } else {
                        logger.info("vin:"+vin+"下载失败");
                    }

                } else {
                    logger.info("vin:"+vin+"检测失败");
                }

            } else {
                logger.info("vin:"+vin+"注册失败");
            }


        } catch (Exception e) {
            sr.setSuccessful(false);
            e.printStackTrace();
        } finally {
            sr.sampleEnd();
        }
        return sr;
    }

    public static void main(String[] args) {
        Arguments params = new Arguments();
        params.addArgument("vin", "Performance999999");
        JavaSamplerContext context = new JavaSamplerContext(params);
        DownController test = new DownController();
        test.setupTest(context);
        test.runTest(context);
        test.teardownTest(context);

    }
}